package cn.yuanqiao.web.controller.system;

import cn.yuanqiao.common.constant.CacheConstants;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.service.RedisService;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.system.domain.SysLogininfor;
import cn.yuanqiao.system.service.ITenantLogininforService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/tenantLogininfor")
public class TenantLogininforController extends BaseController
{
    @Autowired
    private ITenantLogininforService logininforService;

    @Autowired
    private RedisService redisService;

    //@RequiresPermissions("system:logininfor:list")
    @GetMapping("/list")
    public TableDataInfo list(SysLogininfor logininfor)
    {
        List<SysLogininfor> list = null;
        String username = SecurityUtils.getUsername();
        if ("sysauditor".equals(SecurityUtils.getUsername())){
            startPage();
            list = logininforService.selectLogininforListBySysauditor(logininfor);
        }else if ("syssso".equals(SecurityUtils.getUsername())){
            startPage();
            list = logininforService.selectLogininforListBySyssso(logininfor);
        }else {
            startPage();
            list = logininforService.selectLogininforList(logininfor);
        }
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    //@RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor)
    {
        List<SysLogininfor> list = null;
        if ("sysauditor".equals(SecurityUtils.getUsername())){
            list = logininforService.selectLogininforListBySysauditor(logininfor);
        }else if ("syssso".equals(SecurityUtils.getUsername())){
            list = logininforService.selectLogininforListBySyssso(logininfor);
        }else {
            list = logininforService.selectLogininforList(logininfor);
        }
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

    //@RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds)
    {
        return toAjax(logininforService.deleteLogininforByIds(infoIds));
    }

    //@RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        logininforService.cleanLogininfor();
        return success();
    }

    //@RequiresPermissions("system:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public AjaxResult unlock(@PathVariable("userName") String userName)
    {
        redisService.deleteObject(CacheConstants.PWD_ERR_CNT_KEY + userName);
        return success();
    }

    /*@InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody SysLogininfor logininfor)
    {
        return toAjax(logininforService.insertLogininfor(logininfor));
    }*/

    @Log(title = "登录日志统计", businessType = BusinessType.SELECT)
    @GetMapping("/loginTongji")
    public AjaxResult loginTongji(SysLogininfor logininfor)
    {
        List<Long[]> list = null;
        String username = SecurityUtils.getUsername();
        if ("sysauditor".equals(SecurityUtils.getUsername())){
            list = logininforService.loginTongjiBySysauditor(logininfor);
        }else if ("syssso".equals(SecurityUtils.getUsername())){
            list = logininforService.loginTongjiBySyssso(logininfor);
        }else {
            list = logininforService.loginTongji(logininfor);
        }
        return AjaxResult.success(list);
    }

    @Log(title = "登录日志审计", businessType = BusinessType.UPDATE)
    @PostMapping("/upAudit")
    public AjaxResult upAudit(@RequestBody JSONObject jsonObject)
    {
       // if ("sysauditor".equals(SecurityUtils.getUsername())){
            logininforService.updatAuditByIds(jsonObject);
      //  }
        return success();
    }
}
